/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mrp.productionPlanning.session;

import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import mrp.productionPlanning.entity.ForecastEntity;
import mrp.productionPlanning.entity.ForecastTableEntity;
import mrp.productionPlanning.entity.HistoricalDataEntity;

/**
 *
 * @author GKnight
 */
@Stateless
@LocalBean
public class ForecastTableSessionBean {

    @PersistenceContext
    private EntityManager em;
    
    @EJB
    HistoricalDataSessionBean historyDataManager = new HistoricalDataSessionBean();
    @EJB
    ForecastSessionBean forecastManager = new ForecastSessionBean();
    
    public ForecastTableEntity createForecastTable(String monthName, int yearName){
        ForecastTableEntity forecastTable = new ForecastTableEntity();
        
        ForecastEntity forecast = new ForecastEntity();
        forecast.setMonthName(monthName);
        forecast.setYearName(yearName+1);
        forecastTable.setForecast(forecast);
        
        HistoricalDataEntity historicalData = historyDataManager.getHistoricalDataByMonth(monthName,yearName);
        forecastTable.setHistoricalData(historicalData);

        forecastTable.setYearFor(yearName+1);
        forecastTable.setMonthName(monthName);
        
//        em.persist(forecastTable);
        return forecastTable;
    }
    
    public void saveForecastTable(ForecastTableEntity forecastTable){
        em.persist(forecastTable);
    }
    
    public List<ForecastTableEntity> getForecastTableByYear(int yearName){
        Query query = em.createQuery("SELECT f FROM ForecastTableEntity f WHERE f.yearFor = :yearName");
        query.setParameter("yearName", yearName);
        List<ForecastTableEntity> forecastTableList = query.getResultList();
        return forecastTableList;
    }
}
